<template>
    <el-form ref="ruleFormRef" style="width: 100%" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon >
        <el-form-item label="名称" prop="title">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="交底部位" prop="type_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="施工班组" prop="region_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="交底日期" prop="process_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="交底人" prop="region_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="接受人" prop="process_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="安全员" prop="process_id">
            <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="责任人" prop="person_id">
            <component :is="userSelect" :data=ruleForm.person_id @onUserSelect="onUserSelect" />
        </el-form-item>
        <el-form-item label="交底内容" prop="content">
            <div><TEditor v-model:content="ruleForm.content" /></div>
        </el-form-item>
    </el-form>
    <el-divider />
    <div class="text-center">
        <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="loading"> 保存 </el-button>
    </div>
</template>
  
<script lang="ts" setup>
import { reactive, ref,watch } from 'vue'
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from 'element-plus'
import userSelect from "@/components/userSelect.vue";
import http from '../../../../util/request';
const loading = ref<boolean>(false)

interface RuleForm {
    id: number
    title: string
    project_id:number
    type_id:number
    created_at: string
    floor: string
    plan_id:number
    person_id:number
    content: string
}

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<RuleForm>({
    id: 0,
    project_id:0,
    title: '',
    created_at: '',
    floor: '',
    person_id:0,
    type_id:0,
    plan_id:0,
    content: ''
})

const onGetData = () => {
    if(!ruleForm.value.id){
        return;
    }
    http.get('/safe/'+ruleForm.value.id).then((response:any)=>{
        if(response.data.data){
            ruleForm.value = response.data.data
        }
    })
}

const props = defineProps<{
    data:any
}>()

watch(props,()=>{
    ruleForm.value.id = props.data
    onGetData()
},{ immediate: true })

const rules = reactive<FormRules<RuleForm>>({
    title: [
        { required: true, message: '请输入检查标识', trigger: 'blur' },
        { min: 2, max: 30, message: '范围在2-30字之间', trigger: 'blur' },
    ],
    project_id: [
        { required: true, message: '请选择项目', trigger: 'blur' },
    ],
    floor: [
        { required: true, message: '请输入楼层', trigger: 'blur' },
    ],
    type_id: [
        { required: true, message: '请选择检查类别', trigger: 'blur' },
    ],
    person_id: [
        { required: true, message: '请选择责任人', trigger: 'blur' },
    ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
    console.log(ruleForm.value)
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            loading.value = true
            let request:any = null
            if(ruleForm.value.id){
                request = http.patch('/safe',ruleForm.value)
            }else{
                request = http.put('/safe',ruleForm.value)
            }
            request.then((response:any)=>{
                loading.value = false
                if(response.data.code != 200){
                    ElMessage.error(response.data.msg);
                    return
                }
                onSave()
            })      
        } else {
            console.log('error submit!', fields)
        }
    })
}

const onUserSelect = (data:number) => {
    ruleForm.value.person_id = data
}

const onSave = () => {
    emit('onSave')
}

const emit = defineEmits(['onSave'])
</script>
<style scoped>
</style>
